package com.cssw.fyzb.busi.service.video;

import com.cssw.fyzb.core.dao.video.UserVideoDao;
import com.cssw.fyzb.core.eao.video.VideoEao;
import com.cssw.fyzb.core.mao.video.VideoStatMao;
import com.cssw.fyzb.core.model.dodb.CmfUserMusic;
import com.cssw.fyzb.core.model.dodb.CmfUserVideo;
import com.cssw.fyzb.core.model.mos.VideoStatMo;
import com.cssw.fyzb.core.util.StringUtil;
import com.ydn.dbframe.plugin.activerecord.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

/**
 * @author Feng Chen
 */
@Service
public class VideoStatServiceImpl implements VideoStatService {

    @Autowired
    private VideoStatMao statMao;

    @Autowired
    private VideoEao videoEao;

    @Autowired
    private UserVideoDao userVideoDao;

    @Override
    public VideoStatMo findByVideoId(Long videoId) {
        return statMao.findByVideoId(videoId);
    }

    @Override
    public void incLikes(Long videoId) {
        statMao.incLikes(videoId);
    }

    @Override
    public void decLikes(Long videoId) {
        statMao.decLikes(videoId);
    }

    @Override
    public void incViews(Long videoId) {
        statMao.incViews(videoId);
    }

    @Override
    public void incComments(Long videoId) {
        statMao.incComments(videoId);
    }

    @Override
    public void incShares(Long videoId) {
        statMao.incShares(videoId);
    }

    /**
     * 获取音频
     *
     * @return com.cssw.fyzb.core.model.dodb.CmfUserMusic
     * @Param [musicId]
     **/
    public CmfUserMusic getCmfUserMusic(int musicId) {
        return userVideoDao.getCmfUserMusic(musicId);
    }


    /**
     * 存入es
     *
     * @return
     * @Param
     **/
    public void save(
            Long userId, Long vedioId, String title, String media, String cover, String city, String lat, String lng, Integer createTime
            , Integer musicId, String musicTitle, String musicAuthor, String musicImgUrl, String musicLength, String musicFileUrl,
            String musicFormat, String anyway, Integer classId) {
        videoEao.save(userId, vedioId, title, media, cover, city, lat, lng, createTime
                , musicId, musicTitle, musicAuthor, musicImgUrl, musicLength, musicFileUrl,
                musicFormat, anyway, classId);
    }

    public CmfUserVideo getCmfUserVideo(Long id) {
        return userVideoDao.getCmfUserVideo(id);
    }

    @Override
    public long deleteById(Long id, Date createDate) {
        return videoEao.deleteById(id, createDate);
    }

    @Override
    public void syncToEs(Long videoId, String type) {
        if (StringUtil.isNotEmpty(type) && "all".equals(type)){
            int pageNum = 1;
            int pageSize = 100;

            while (true){
                Page<CmfUserVideo> pages = userVideoDao.pageByAuditStatus(1, pageNum, pageSize);
                if (pages.getList() == null || pages.getList().size() <= 0){
                    break;
                }
                pageNum++;

                for (CmfUserVideo cmfUserVideo: pages.getList()){
                    syncToEs(cmfUserVideo);
                }

            }
        } else{
            CmfUserVideo userVideo = userVideoDao.findById(videoId);
            syncToEs(userVideo);
        }
    }

    private void syncToEs(CmfUserVideo userVideo){
        if (null == userVideo){
            return;
        }
        CmfUserMusic userMusic = null;
        if (null != userVideo && userVideo.getMusicId() != 0){
            userMusic = userVideoDao.getCmfUserMusic(userVideo.getMusicId());
        }
        if (userMusic == null){
            userMusic = new CmfUserMusic();
        }
        long addTime = Long.valueOf(userVideo.getAddtime()) * 1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(addTime);
        videoEao.save(Long.valueOf(userVideo.getUid()), userVideo.getId(), userVideo.getTitle(), userVideo.getHref(), userVideo.getThumb()
                , userVideo.getCity(), userVideo.getLat(), userVideo.getLng(), userVideo.getAddtime()
                , userVideo.getMusicId(), userMusic.getTitle(), userMusic.getAuthor(), userMusic.getImgUrl(), userMusic.getLength()
                , userMusic.getFileUrl(), userMusic.getTitle(), userVideo.getAnyway(), userVideo.getClassid());
    }
}
